package com.meihua.baoleyi.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.request.BaseRequest;
import com.meihua.baoleyi.adapters.BaseNoDateAdaper;
import com.meihua.baoleyi.adapters.BaseNoWifiAdaper;
import com.meihua.baoleyi.view.BaseListView;
import com.meihua.baoleyi.view.IRecyclerView;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by liming  2017/10/18
 */

public class MyStringCallBack extends StringCallback {
    private BaseListView listView;
    private Context context;
    private IRecyclerView v;
    private boolean isfootShow = true;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            setListViewHeaderforwifi();
        }
    };

    public MyStringCallBack(BaseListView listView, Context context) {
        this.listView = listView;
        this.context = context;
    }

    public MyStringCallBack(IRecyclerView listView, Context context) {
        this.v = listView;
        this.context = context;
    }

    @Override
    public void onSuccess(String s, Call call, Response response) {
        listView.setDividerHeight(2);
    }

    @Override
    public void parseError(Call call, Exception e) {
        super.parseError(call, e);
        handler.sendEmptyMessage(1);

    }

    @Override
    public void onAfter(String s, Exception e) {
        super.onAfter(s, e);
        if (listView != null) {
            listView.hideFootView(!isfootShow);
            listView.stopLoadMore();
            listView.stopRefresh();
        } else if (v != null) {
            v.setRefreshing(false);

        }
    }

    @Override
    public void onBefore(BaseRequest request) {
        super.onBefore(request);
    }

    /**
     * 没网情况下显示的
     */
    public void setListViewHeaderforwifi() {
        isfootShow = false;
        if (listView != null) {
            listView.hideFootView(true);listView.setDividerHeight(0);
            BaseNoWifiAdaper adaper = new BaseNoWifiAdaper(context, 2);
            listView.setAdapter(adaper);
        } else if (v != null) {
            BaseNoDateAdaper adaper = new BaseNoDateAdaper(context, 2);
            v.setIAdapter(adaper);
        }
    }

    /**
     * 没有数据显示的
     */
    public void setListViewHeaderfordate() {
        isfootShow = false;
        if (listView != null) {
            listView.hideFootView(true);
            listView.setDividerHeight(0);
            BaseNoWifiAdaper adaper = new BaseNoWifiAdaper(context, 1);
            listView.setAdapter(adaper);
        } else if (v != null) {
//            Log.e("...", "..setListViewHeaderfordate.recView.");
            BaseNoDateAdaper adaper = new BaseNoDateAdaper(context, 1);
            v.setIAdapter(adaper);
        }
    }
}
